package kxfang.com.common.views.dialog;


import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

import androidx.databinding.DataBindingUtil;

import kxfang.com.common.R;
import kxfang.com.common.constant.Constant;
import kxfang.com.common.databinding.DialogPayBinding;
import kxfang.com.common.utils.UITools;


/**
 * 支付
 */
public class PayDialog extends PopupWindow {

    private Context context;
    private PayListener listener;
    private boolean isWxCheck = true;
    private DialogPayBinding binding;
    private String price;

    public PayDialog(Context context, PayListener listener) {
        super(context);
        this.context = context;
        this.listener = listener;
        this.setOutsideTouchable(false);
        this.setFocusable(true);
        this.setTouchable(true);
//        this.setClippingEnabled(false);
        this.setHeight(LinearLayout.LayoutParams.MATCH_PARENT);
        this.setWidth(LinearLayout.LayoutParams.MATCH_PARENT);
        this.setBackgroundDrawable(new BitmapDrawable());
        UITools.darkenBackground(this.context,0.5f);
        init();
    }

    public PayDialog(Context context, PayListener listener, String price) {
        super(context);
        this.price = price;
        this.context = context;
        this.listener = listener;
        this.setOutsideTouchable(false);
        this.setFocusable(true);
        this.setTouchable(true);
//        this.setClippingEnabled(false);
        this.setHeight(LinearLayout.LayoutParams.MATCH_PARENT);
        this.setWidth(LinearLayout.LayoutParams.MATCH_PARENT);
        this.setBackgroundDrawable(new BitmapDrawable());
        UITools.darkenBackground(this.context,0.5f);
        init();
    }

    private void init() {
        binding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.dialog_pay, null, false);
        setContentView(binding.getRoot());
        binding.wxLayout.setOnClickListener(v -> {
            isWxCheck = true;
            updateView();
        });
        binding.aliLayout.setOnClickListener(v -> {
            isWxCheck = false;
            updateView();
        });
        binding.btnPay.setOnClickListener(v -> {
            dismiss();
            UITools.darkenBackground(context,1f);
            listener.onPay(isWxCheck ? Constant.PayConstants.WX_PAY : Constant.PayConstants.ALI_PAY);
        });
        binding.payClose.setOnClickListener(v -> {
            dismiss();
            UITools.darkenBackground(context,1f);
        });
        if (!TextUtils.isEmpty(price)){
            binding.tvPrice.setVisibility(View.VISIBLE);
            binding.tvPrice.setText("¥".concat(price));
        }
    }

    private void updateView() {
        binding.wxCheck.setImageResource(isWxCheck ? R.drawable.check : R.drawable.un_check);
        binding.aliPay.setImageResource(isWxCheck ? R.drawable.un_check : R.drawable.check);
    }

    public interface PayListener {
        /**
         * 唤起支付
         * 从constant里面的PayConstants获取相关信息
         *
         * @param type 1，微信；2,支付宝
         */
        void onPay(int type);
    }
}
